package database;

import database.customer.CustomerCURDGUI;
import database.employee.EmployeeCURDGUI;
import database.employee.tb_employee;
import database.good.GoodCURDGUI;
import database.paydetail.PaydetailCURDGUI;
import database.paymain.PaymainCURDGUI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class GUImanager {
     JFrame frame;
    private JMenuBar menuBar;
    private JMenu fileMenu, infoMenu,AccountMenu;
    private JMenuItem employeeItem, productItem, customerItem;
    private JMenuItem purchaseMainItem, purchaseDetailItem;
    private JMenuItem AccountItem;

    public GUImanager(tb_employee e1) {
        frame = new JFrame("超市管理系统");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 800);
        frame.setLocationRelativeTo(null); // 居中显示

        // 创建菜单栏
        menuBar = new JMenuBar();

        // 创建顶层菜单
        fileMenu = new JMenu("采购信息");
        infoMenu = new JMenu("基本信息");
        AccountMenu =new JMenu("账号信息");
        // 创建菜单项
        employeeItem = new JMenuItem("员工信息");
        productItem = new JMenuItem("商品信息");
        customerItem = new JMenuItem("客户信息");
        purchaseMainItem = new JMenuItem("采购主表");
        purchaseDetailItem = new JMenuItem("采购详细表");
        AccountItem=new JMenuItem("账号信息");

        //给菜单键添加监听
        employeeItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new EmployeeCURDGUI(e1);// 展示员工信息
                    frame.dispose();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        productItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new GoodCURDGUI(e1);
                    frame.dispose();//展示商品信息
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        customerItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new CustomerCURDGUI(e1); //展示客户信息
                    frame.dispose();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        purchaseMainItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new PaymainCURDGUI(e1); //展示采购主表
                    frame.dispose();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        purchaseDetailItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    new PaydetailCURDGUI(e1); //展示采购明细表
                    frame.dispose();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        AccountItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
               new EmployeeInfoPanel(e1);
            }
        });
        AccountMenu.add(AccountItem);

        // 将菜单项添加到信息菜单中
        infoMenu.add(employeeItem);
        infoMenu.add(productItem);
        infoMenu.add(customerItem);

        // 将采购相关的菜单项添加到文件菜单中
        fileMenu.add(purchaseMainItem);
        fileMenu.add(purchaseDetailItem);

        // 将菜单添加到菜单栏
        menuBar.add(fileMenu);
        menuBar.add(infoMenu);
        menuBar.add(AccountMenu);
        // 将菜单栏添加到框架
        frame.setJMenuBar(menuBar);

        // 设置框架可见
        frame.setVisible(true);
    }
}




